package com.pignest.hotel.ui.room;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Log;
import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.RoomType;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.L;
import com.pignest.hotel.util.UIUtils;

public class RoomTypeWindow extends Window {
	private static final long serialVersionUID = 2147239767923279155L;
	
	private boolean add;	//or edit
	private RoomType roomType;
	private RoomService service;
	
	private Textbox nameTextbox;
	private Textbox shortNameTextbox;
	private Listbox capacityListbox, colorListbox;
	
	private Textbox priceTextbox, minPriceTextbox;
	private Textbox hourPriceTextbox, minHourPriceTextbox;
	private Textbox halfDayPriceTextbox, minHalfDayPriceTextbox;
	
	private Textbox remarkTextbox;
	
	public RoomTypeWindow() {
		service = Services.getRoomService();
		add = true;
	}
	
	private void init() {
		if (nameTextbox != null)
			return;
		nameTextbox = (Textbox)getFellow("name");
		shortNameTextbox = (Textbox)getFellow("shortName");
		capacityListbox = (Listbox)getFellow("capacity");
		colorListbox = (Listbox)getFellow("color");
		priceTextbox = (Textbox)getFellow("price");
		minPriceTextbox = (Textbox)getFellow("minPrice");
		hourPriceTextbox = (Textbox)getFellow("hourPrice");
		minHourPriceTextbox = (Textbox)getFellow("minHourPrice");
		halfDayPriceTextbox = (Textbox)getFellow("halfDayPrice");
		minHalfDayPriceTextbox = (Textbox)getFellow("minHalfDayPrice");
		remarkTextbox = (Textbox)getFellow("remark");
		if (!add) {
			nameTextbox.setValue(roomType.getName());
			shortNameTextbox.setValue(roomType.getShortName());
			remarkTextbox.setValue(roomType.getRemark());
			priceTextbox.setValue(Utils.toPrice(roomType.getPrice()));
			minPriceTextbox.setValue(Utils.toPrice(roomType.getMinPrice()));
			hourPriceTextbox.setValue(Utils.toPrice(roomType.getHourPrice()));
			minHourPriceTextbox.setValue(Utils.toPrice(roomType.getMinHourPrice()));
			halfDayPriceTextbox.setValue(Utils.toPrice(roomType.getHalfDayPrice()));
			minHalfDayPriceTextbox.setValue(Utils.toPrice(roomType.getMinHalfDayPrice()));
			UIUtils.setSelectedItem(capacityListbox, String.valueOf(roomType.getCapacity()));
			UIUtils.setSelectedItem(colorListbox, roomType.getColor());
		}
	}
	
	public String onSave() {
		String error = check();
		if (error != null)
			return error;
		try {
			String name = nameTextbox.getValue().trim();
			String shortName = StringUtils.defaultIfEmpty(shortNameTextbox.getValue().trim(), null);
			int capacity = Utils.getInteger((String)UIUtils.getSelectedValue(capacityListbox), 1);
			String color = UIUtils.getSelectedValue(colorListbox);
			int price = Utils.toPrice(priceTextbox.getValue());
			int minPrice = Utils.toPrice(minPriceTextbox.getValue());
			int hourPrice = Utils.toPrice(hourPriceTextbox.getValue());
			int minHourPrice = Utils.toPrice(minHourPriceTextbox.getValue());
			int halfDayPrice = Utils.toPrice(halfDayPriceTextbox.getValue());
			int minHalfDayPrice = Utils.toPrice(minHalfDayPriceTextbox.getValue());
			String remark = StringUtils.defaultString(remarkTextbox.getValue(), null);
			
			RoomType type = add ? new RoomType() : service.getRoomType(roomType.getId());
			type.setName(name);
			type.setShortName(shortName);
			type.setShortName(shortName);
			type.setCapacity(capacity);
			type.setColor(color);
			type.setPrice(price);
			type.setMinPrice(minPrice);
			type.setHourPrice(hourPrice);
			type.setMinHourPrice(minHourPrice);
			type.setHalfDayPrice(halfDayPrice);
			type.setMinHalfDayPrice(minHalfDayPrice);
			type.setRemark(remark);
			service.insertOrUpdate(type);
			if (add) {
				L.insert(type);
			} else {
				L.update(type);
			}
		} catch (Exception e) {
			Log.e(e);
			return "内部错误";
		}
		return null;
	}
	
	private String check() {
		if (UIUtils.getSelectedIndex(capacityListbox) == -1)
			return "容量";
		if (UIUtils.getSelectedIndex(colorListbox) == -1)
			return "颜色";
		String name = nameTextbox.getValue();
		if (StringUtils.isBlank(name))
			return "名称不能为空";
		if (!Utils.checkPrice(priceTextbox.getValue()) || !Utils.checkPrice(minPriceTextbox.getValue()) || !Utils.checkPrice(hourPriceTextbox.getValue()) || !Utils.checkPrice(minHourPriceTextbox.getValue())) {
			return "请填写正确的价格";
		}
		int p = Utils.toPrice(priceTextbox.getValue());
		if (p < 0 || p > 10000000)
			return "价格不合理";
		int m = Utils.toPrice(minPriceTextbox.getValue());
		if (m < 0 || m > p)
			return "最低价格不合理";
		int h = Utils.toPrice(hourPriceTextbox.getValue());
		if (h < 0 || h > p)
			return "钟点价格不合理";
		int mh = Utils.toPrice(minHourPriceTextbox.getValue());
		if (mh < 0 || mh > h)
			return "钟点最低价格不合理";
		int f = Utils.toPrice(halfDayPriceTextbox.getValue());
		if (h < 0 || f > p || f < h)
			return "半天房价格不合理";
		int mf = Utils.toPrice(minHalfDayPriceTextbox.getValue());
		if (mf < 0 || mf > f || mf < mh)
			return "最低半天房价不合理";
		
		if (add) {
			if (service.getRoomTypeByName(name) != null)
				return "名称已经存在";
		} else {
			if (!name.equals(roomType.getName()) && service.getRoomTypeByName(name) != null)
				return "名称已经存在";
		}
		return null;
	}
	
	public void setRoomTypeId(String roomTypeId) {
		roomType = roomTypeId == null ? null : service.getRoomType(roomTypeId);
		if (roomType != null) {
			this.add = false;
			setTitle("编辑房间类型");
		} else {
			this.add = true;
			setTitle("新增房间类型");
		}
		init();
	}
}
